package controllers

import (
	"encoding/json"
	"fmt"
	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
	"godemo/models"
	"godemo/pkg/logger"
	"strconv"
)

type UserController struct{}

type UserLoginVO struct {
	Name string `json:"name"`
	Id   int    `json:"id"`
}

func (u UserController) GetUserInfo(c *gin.Context) {
	idStr := c.Param("id")
	name := c.Param("name")
	// 自定义日志
	logger.Write("获取个人信息入参", "user")

	id, _ := strconv.Atoi(idStr)
	user, _ := models.GetUserTest(id)
	ReturnSuccess(c, 0, name, user, 1)
}

func (u UserController) AddUser(c *gin.Context) {
	name := c.PostForm("name")
	id, err := models.AddUser(name)
	if err != nil {
		ReturnError(c, 4002, "保存出错")
		return
	}
	ReturnSuccess(c, 0, "保存成功", id, 1)
}

func (u UserController) UpdateUser(c *gin.Context) {
	name := c.PostForm("name")
	idStr := c.PostForm("id")
	id, _ := strconv.Atoi(idStr)

	models.UpdateUser(id, name)
	ReturnSuccess(c, 0, "更新成功", true, 1)
}

func (u UserController) DeleteUser(c *gin.Context) {
	idStr := c.PostForm("id")
	id, _ := strconv.Atoi(idStr)

	err := models.DeleteUser(id)
	if err != nil {
		ReturnError(c, 4001, "删除出错")
		return
	}

	ReturnSuccess(c, 0, "删除成功", true, 1)
}

func (u UserController) GetUserList(c *gin.Context) {
	users, err := models.GetUserList()
	if err != nil {
		ReturnError(c, 4000, "获取失败")
		return
	}
	ReturnSuccess(c, 0, "成功", users, int64(len(users)))
}

func (u UserController) GetList(c *gin.Context) {
	id := c.PostForm("id")
	name := c.PostForm("name")
	ReturnSuccess(c, 0, name, id, 1)
}

func (u UserController) AddList(c *gin.Context) {
	/*defer func() {
		if err := recover(); err != nil {
			fmt.Println("捕获异常:", err)
		}
	}()*/

	num1 := 1
	num2 := 0
	num3 := num1 / num2

	ReturnError(c, 4004, num3)
}

func (u UserController) Register(c *gin.Context) {
	name := c.PostForm("name")
	pwd := c.PostForm("pwd")
	confirmPwd := c.PostForm("confirmPwd")

	if name == "" || pwd == "" || confirmPwd == "" {
		ReturnError(c, -1, "请填写完整信息")
		return
	}
	if pwd != confirmPwd {
		ReturnError(c, -1, "两次密码不一致")
		return
	}

	user, err := models.FindUserByName(name)

	jsonBytes, _ := json.Marshal(user)
	jsonStr := string(jsonBytes)
	logger.Write("查询用户信息："+jsonStr, "user")
	if user.Id > 0 {
		ReturnError(c, -1, "用户已注册")
		return
	}

	userId, err := models.SaveUser(name, Md5Encode(pwd))
	if err != nil {
		ReturnError(c, -1, "保存失败")
		return
	}
	ReturnSuccess(c, 200, "成功", userId, 1)
}

func (u UserController) Login(c *gin.Context) {
	name := c.PostForm("name")
	pwd := c.PostForm("pwd")

	user, err := models.FindUserByName(name)
	if err != nil {
		ReturnError(c, 500, "账号未注册")
		return
	}

	if user.Password != Md5Encode(pwd) {
		ReturnError(c, 500, "密码错误")
		return
	}

	// 登录
	data := UserLoginVO{Name: user.Name, Id: user.Id}
	fmt.Println(data)
	dataBytes, _ := json.Marshal(data)

	session := sessions.Default(c)
	session.Set("login:"+strconv.Itoa(user.Id), string(dataBytes))
	err = session.Save()
	if err != nil {
		fmt.Println(err)
		ReturnError(c, 0, "session保存失败")
		return
	}
	ReturnSuccess(c, 200, "成功", data, 1)

}
